package com.ssm.song.book.controller;

import com.ssm.song.book.util.Result;
import com.ssm.song.book.util.StatusCode;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @Author 宋伟宁
 * @Date 2023/10/16
 * @Version 1.0
 **/
@RestControllerAdvice
public class GloblaExecption {

    @ExceptionHandler(NullPointerException.class)
    public Result nullHandling(NullPointerException e){
      return   new Result<>(StatusCode.ERROR,"发生了空指针异常",false);
    }

    @ExceptionHandler(SQLException.class)
    public Result sqlExceptionHandler(SQLException ex) {
        if (ex instanceof SQLIntegrityConstraintViolationException) {
            return new Result(StatusCode.ERROR, "该数据存在关联数据，删除失败！",false);
        }
        return new Result(StatusCode.ERROR,  "数据库操作异常！",false);
    }
}
